package com.itheima.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;


import java.util.*;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Map> findmenu(String username) {
        List<Map> map =new ArrayList<>();
        //根据用户名查询用户
        User user = userDao.findByUsername(username);
        //根据用户id查询角色
        Set<Role> role = roleDao.findByUserId(user.getId());
        for (Role role1 : role) {
            //遍历角色拿到角色对应的主菜单
            List<Menu> pMenu = menuDao.findPMenu(role1.getId());
            for (Menu menu : pMenu) {
                Map pmenuMap = new HashMap();//封装父菜单
                pmenuMap.put("path",menu.getPath());
                pmenuMap.put("name",menu.getName());
                pmenuMap.put("icon",menu.getIcon());
                List zmenulist = new ArrayList();
                //通过主菜单拿到子菜单对象
                List<Menu> zmenu = menuDao.findZmenu(menu.getId());
                for (Menu o : zmenu) {//遍历子菜单对象
                    //在通过子菜单id查询下面是否还有子菜单
                    //List<Menu> zmenu1 = menuDao.findZmenu(o.getId());
                    Map zmap = new HashMap();//封装子菜单
                    //如果子菜单下没有了,
                    zmap.put("path",o.getPath());
                    zmap.put("name",o.getName());
                    zmap.put("linkUrl",o.getLinkUrl());
                    zmap.put("children",null);
                    zmenulist.add(zmap);
                }
                pmenuMap.put("children",zmenulist);
                map.add(pmenuMap);
            }
        }
        return map;
    }
    @Override
    public List<Map<String, Object>> findAll(String username) {
       /* List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        User user = userDao.findByUsername(username);
        Integer id = user.getId();
        Set<Role> roleSet = roleDao.findByUserId(id);
        for (Role role : roleSet) {
            Integer roleId = role.getId();
           List<Menu> menu = menuDao.findById(roleId);
            for (Menu menu1 : menu) {
                menuDao.findChildrenById(menu1.getId());
                menu.add(menu1);
            }

            map.put("menu",menu);
        }
        for (Map<String, Object> objectMap : list) {
           map.put("objectMap",objectMap);
        }
            list.add(map);
*/
        return null;
    }
}
